package com.atguigu.gmall.logger.gmalllogger.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.common.logger.GmallConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * tomcat日志数据进入kafka
 *
 * @author WangJX
 * @version 1.0
 * @date 2019/11/25 19:16
 */
@RestController
@Slf4j
public class LoggerController {

    @Autowired
    KafkaTemplate<String, String> kafkaTemplate;

    @PostMapping
    public String doLog(@RequestParam("logString") String logString ) {

        JSONObject jsonObject = JSON.parseObject(logString);
        jsonObject.put("ts", System.currentTimeMillis());

        String logger = jsonObject.toJSONString();

        //一份数据落盘，一份数据到kafka
        log.info(logger);

        //判断数据是start日志还是event日志
//        kafkaTemplate.send("first", logger);
        if (jsonObject.getString("type").equalsIgnoreCase("startup")) {
            kafkaTemplate.send(GmallConstants.KAFKA_TOPIC_STARTUP, logger);
        } else {
            kafkaTemplate.send(GmallConstants.KAFKA_TOPIC_EVENT, logger);
        }

        return "sucess";
    }
}
